<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Admin_Googlelanguages extends Admin_Controller {
	public function __construct()
	{
		parent::__construct();
		$this->lang->load('googlelanguage');
		$this->load->library('Google_language_entity');
	}
	public function ajax_googlelanguage_get(){
		$googlelanguageEntity =  new CI_Google_language_entity();
		$googlelanguage = $googlelanguageEntity->getGoogleLanguageByCode($_REQUEST['code']);
		echo json_encode($googlelanguage);
	}
	public function ajax_googlelanguage_list(){
		$googlelanguageEntity =  new CI_Google_language_entity();
		$escortSettings = new CI_Escort_setting_entity();
		$defaultLanguageId = $escortSettings->getDefaultLanguageId();
		$googlelanguageList = $googlelanguageEntity->getGoogleLanguageListByLanguage($defaultLanguageId);
		$data['googlelanguageList'] = $googlelanguageList;
		
		$languageEntity = new CI_Language_entity();
		$languageList = $languageEntity->getLanguageList();
		$data['languageList'] = $languageList;
		
		$this->load->view('admin/googlelanguages/index', $data);
	}
	public function ajax_googlelanguage_save(){
		$googlelanguageNameList = $_REQUEST['googlelanguage'];
		$googlelanguageNameDefault = '';
		foreach ($googlelanguageNameList as $key => $value) {
			if($value != null && $value != ""){
				$googlelanguageNameDefault = $value;
				break;
			}
		}
		$result = array();
		if($googlelanguageNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_googlelanguage_require');
		}else{
			if(!$_REQUEST['code']){
				$result['result'] = false;
				$result['message'] = lang('escort_googlelanguage_code_required');
			}else if($this->checkExistedCode(0, $_REQUEST['code'])){
				$result['result'] = false;
				$result['message'] = lang('escort_googlelanguage_existed');
			}else{
				$code_f_id = 0;
				$code = $_REQUEST['code'];
				foreach ($googlelanguageNameList as $key => $value) {
					$googlelanguageEntity =  new CI_Google_language_entity();
					if($value != null && $value != ''){
						$googlelanguageEntity->setLanguage($value);
					}else{
						$googlelanguageEntity->setLanguage($googlelanguageNameDefault);
					}
					$googlelanguageEntity->setLanguageId($key);
					$googlelanguageEntity->setCode($code);
					$googlelanguageEntity->setCodeFId($code_f_id);
					$id = $googlelanguageEntity->save();
					
					if($code_f_id == 0){
						$code_f_id = $id;
						$googlelanguageEntity->setCodeFId($code_f_id);
						$googlelanguageEntity->update();
					}
					
				}
				$result['result'] = true;
				$result['message'] =lang('escort_googlelanguage_save_success');
				$result['id'] =$id;
			}
		}
		echo json_encode($result);
	}
	public function ajax_googlelanguage_update(){
		$googlelanguageNameList = $_REQUEST['googlelanguage'];
		$googlelanguageIdList = $_REQUEST['googlelanguage_id'];
		$googlelanguageCode = $_REQUEST['code'];
		$googlelanguageNameDefault = '';
		foreach ($googlelanguageNameList as $key => $value) {
			if($value != null && $value != ""){
				$googlelanguageNameDefault = $value;
				break;
			}
		}
		$result = array();
		if(!$_REQUEST['code']){
			$result['result'] = false;
			$result['message'] = lang('escort_googlelanguage_code_required');
		}else{
			$keys = array_keys($googlelanguageIdList);
			$code_f_id = 0;
			foreach ($googlelanguageIdList as $key => $value){
				if($value){
					$code_f_id = $value;
					break;
				}
			}
			foreach ($googlelanguageNameList as $key => $value) {
				$googlelanguageEntity =  new CI_Google_language_entity();
				if($value != null && $value != ''){
					$googlelanguageEntity->setLanguage($value);
				}else{
					$googlelanguageEntity->setLanguage($googlelanguageNameDefault);
				}
				$googlelanguageEntity->setLanguageId($key);
				$googlelanguageEntity->setCode($googlelanguageCode);
				
				$googlelanguageEntityTemp =  new CI_Google_language_entity();
				if($googlelanguageIdList[$key]){
					if($code_f_id == 0){
						$code_f_id = $googlelanguageIdList[$key];
					}
					$googlelanguageEntity->setCodeFId($code_f_id);
					$googlelanguageEntity->setId($googlelanguageIdList[$key]);
					$googlelanguageEntity->update();
				}else{
					$googlelanguageEntity->setCodeFId($code_f_id);
					$id = $googlelanguageEntity->save();
					if($code_f_id == 0){
						$code_f_id = $id;
					}
					$googlelanguageEntity->setCodeFId($code_f_id);
					$googlelanguageEntity->setId($id);
					$googlelanguageEntity->update();
				}	
			}
			$result['result'] = true;
			$result['message'] =lang('escort_googlelanguage_save_success');
		}
		
		echo json_encode($result);
	}
	public function ajax_googlelanguage_delete(){
		$result = array();
		$googlelanguageEntity =  new CI_Google_language_entity();
		$result['result'] = $googlelanguageEntity->deleteByCode($_REQUEST['code']);
		if($result['result']){
			$result['message'] = lang('escort_googlelanguage_delete_success');
		}else{
			$result['message'] = lang('escort_googlelanguage_delete_error');
		}
		echo json_encode($result);
	}
	function checkExistedCode($id, $name){
		$googlelanguageEntity =  new CI_Google_language_entity();
		return $googlelanguageEntity->checkExistedCode($id, $name);
	}
}
?>